package com.activiti.dao.hibernate;

import com.activiti.dao.ActivitiDao;
import com.activiti.dao.ActivitiSubDao;
import com.activiti.dao.model.Activiti_SubTaskInfo;
import com.activiti.dao.model.Activiti_SubTaskInfoRO;
import com.activiti.dao.model.Activiti_TaskInfo;
import com.activiti.dao.model.Activiti_TaskInfoRO;
import com.erp.hr.dao.model.HrDepartment;
import org.redragon.framework.annotation.Permissions;
import org.redragon.framework.annotation.Permissions.PermissionType;
import org.redragon.framework.annotation.SqlParam;
import org.redragon.framework.hibernate.DaoSupport;
import org.redragon.framework.hibernate.model.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class ActivitiSubDaoImpl implements ActivitiSubDao {

    //注入DaoSupport工具类
    @Autowired
    private DaoSupport daoSupport;
    
    @Override
    public void insertDataObject(Activiti_SubTaskInfo obj) {
        this.daoSupport.insertDataTransaction(obj);
    }

    @Override
    public void updateDataObject(Activiti_SubTaskInfo activiti_subTaskInfo) {
        this.daoSupport.updateDataTransaction(activiti_subTaskInfo);
    }

    @Override
    public void insertOrUpdateDataObject(Activiti_SubTaskInfo activiti_subTaskInfo) {
        this.daoSupport.insertOrUpdateDataTransaction(activiti_subTaskInfo);
    }

    @Override
    public void deleteDataObject(Activiti_SubTaskInfo activiti_subTaskInfo) {
        this.daoSupport.deleteDataTransactionJPA(activiti_subTaskInfo);
    }

    @Override
    public List<Activiti_SubTaskInfo> getDataObjects() {
        return this.daoSupport.getDataAllObject(Activiti_SubTaskInfo.class);
    }


    @Override
    public Activiti_SubTaskInfo getDataObject(int i) {
        return (Activiti_SubTaskInfo)this.daoSupport.getDataObject(Activiti_SubTaskInfo.class, i);
    }

    @Override
    public Activiti_SubTaskInfo getDataObject(String s) {
        return null;
    }

    @Override
    public List<Activiti_SubTaskInfo> getDataObjects(Activiti_SubTaskInfo activiti_subTaskInfo) {
        return this.daoSupport.getDataAllObject(Activiti_SubTaskInfo.class);
    }

    @Override
    public List<Activiti_SubTaskInfo> getDataObjects(Pages pages) {
        return null;
    }

    @Override
    public List<Activiti_SubTaskInfo> getDataObjects(Pages pages, Activiti_SubTaskInfo activiti_subTaskInfo) {
        return null;
    }

    @Override
    public List<Map<String, Object>> getDataObjectsArray(Pages pages, Activiti_SubTaskInfo activiti_subTaskInfo) {
        return null;
    }

    @Override
    public List<Activiti_SubTaskInfo> getDataObjectsForDataAuth(String s, Pages pages, Activiti_SubTaskInfo activiti_subTaskInfo) {
        return null;
    }


    @Override
    public List<Activiti_SubTaskInfoRO> getDataObjects(String procInstId) {
        String sql = "select a.*,b.staff_name,c.department_name company_name,d.department_name,b.file from activiti_sub_taskinfo a " +
                "left join hr_staff b on a.staff_code=b.staff_code " +
                "LEFT JOIN hr_department c on a.company_code=c.department_code " +
                "LEFT JOIN hr_department d on a.department_code=d.department_code " +
                "where a.task_id=:task_sub_id order by a.create_time desc";
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("task_sub_id", procInstId);
        List list = this.daoSupport.selectDataSql(sql, Activiti_SubTaskInfoRO.class, args);

        return list;
    }

    @Override
    public List<Activiti_SubTaskInfoRO> getDataObjectsListOnlyLastTrue(String procInstId) {
        String sql = "select a.*,b.staff_name,c.department_name company_name,d.department_name,b.file from " +
                "(SELECT * from activiti_sub_taskinfo a1 where 1=1 and a1.task_id = :task_sub_id and NOT EXISTS (" +
                "SELECT 1 from activiti_sub_taskinfo b where b.task_sub_name = a1.task_sub_name AND b.create_time > a1.create_time AND b.task_id = :task_sub_id)) a " +
                "left join hr_staff b on a.staff_code=b.staff_code " +
                "LEFT JOIN hr_department c on a.company_code=c.department_code " +
                "LEFT JOIN hr_department d on a.department_code=d.department_code " +
                "where a.task_id=:task_sub_id order by a.create_time asc";
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("task_sub_id", procInstId);
        List list = this.daoSupport.selectDataSql(sql, Activiti_SubTaskInfoRO.class, args);
        return list;
    }
}
